package com.hbnu.controller;

import com.hbnu.pojo.Request;
import com.hbnu.pojo.Result;
import com.hbnu.pojo.User;
import com.hbnu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(origins = "*")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/userLogin")
    public Result login(@RequestBody Request rq){
        User user=userService.login(rq);
        if(user!=null){
            return Result.success(user);
        }
        else return Result.error("账号密码错误");
    }

    @PostMapping("/userRegister")
    public Result register(@RequestBody Request rq){
        try{
            userService.register(rq);
            return Result.success();
        }catch (Exception exception){
            return Result.error("账号重复");
        }
    }

    @PostMapping("/updateUserUname")
    public Result updateUserUname(@RequestBody Request rq){
        userService.updateUserUname(rq);
        return Result.success();
    }

    @PostMapping("/updateUserAddress")
    public Result updateUserAddress(@RequestBody Request rq){
        userService.updateUserAddress(rq);
        return Result.success();
    }
    @PostMapping("/updateUserTelephone")
    public Result updateUserTelephone(@RequestBody Request rq){
        userService.updateUserTelephone(rq);
        return Result.success();
    }
    @PostMapping("/updateUserPassword")
    public Result updateUserPassword(@RequestBody Request rq){
        int res=userService.updateUserPassword(rq);
        if(res==0){
            return Result.error("密码错误");
        }
        else return Result.success();
    }
}
